//设置异步间隔延迟
export function delay(interval = 0) {
    return new Promise(resolve => {
        let timer = setTimeout(_ => {
            clearTimeout(timer);
            resolve();
        }, interval);
    });
};


//把文件按照二进制进行读取
export function readFile(file) {
    return new Promise(resolve => {
        let reader = new FileReader();
        reader.readAsBinaryString(file);
        reader.onload = ev => {
            resolve(ev.target.result);
        };
    });
};

//字段对应表
export let character = {
    realName: {
        text: "真实姓名",
        type: "string"
    },
    username: {
        text: "登录名",
        type: "string"
    },
    phoneNumber: {
        text: "手机号",
        type: "string"
    },
    region: {
        text: "地区",
        type: "string"
    },
    sex: {
        text: "性别",
        type: "string"
    }
};

//字段对应名称处理(真实姓名:realName  电话:phoneNumber)
export function chToES(data) {
    let arr = [];
    data.forEach(item => {
        let obj = [];
        for (let key in character) {
            if (!character.hasOwnProperty(key)) {
                break;
            } else {
                let v = character[key],
                    text = v.text,
                    type = v.type;
                v = item[text] || "";
                type === "string" ? v = String(v) : null;
                type === "number" ? v = Number(v) : null;
                obj[key] = v;
            }
        }
        arr.push(obj);
    });

    return arr;
};
